package br.unb.statistic.function;

public class Integral {

    static final long n = 1000;

    public static double simpson(GenericFunction f, double lowerLimit, double upperLimit) {
        double result = 0;
        double h = (upperLimit - lowerLimit) / n;
        double sum = f.function(lowerLimit);
        for (int i = 1; i < n - 1; i += 2) {
            sum += 4 * f.function(lowerLimit + (i * h));
        }
        for (int i = 2; i < n - 2; i += 2) {
            sum += 2 * f.function(lowerLimit + (i * h));
        }
        result = (h / 3) * sum;
        return result;
    }
}
